package com.kgc.springboot.hangduo.controller;

import com.kgc.springboot.hangduo.pojo.Client;
import com.kgc.springboot.hangduo.pojo.Collect;
import com.kgc.springboot.hangduo.service.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @author: ASUS
 * @date: 2021/1/10 18:22
 * @description:
 */
@Controller
public class CollectController {
    @Autowired
    private CollectService collectService;
    @RequestMapping("/selectAllByClientId")
    public String selectAllByClientId(HttpSession session, Model model){
        Client client = (Client) session.getAttribute("client");
        if(client==null){
            return "user/login";
        }
        List<Collect> collects = collectService.selectAllByClientId(client.getId());
        model.addAttribute("collects",collects);
        return "user-collect";
    }
    @RequestMapping("/addCollect")
    @ResponseBody
    public String select(Integer clauseId,HttpSession session, Model model){
        Client client = (Client) session.getAttribute("client");
        if(client==null){
            //返回登录页面
            System.out.println("**********");
            return "2";
        }
        Collect collect = new Collect();
        collect.setCreateTime(new Date());
        collect.setClientId(client.getId());
        collect.setClauseId(clauseId);
        int i = collectService.addCollect(collect);
        if(i>0){
            return "1";
        }else {
            return "0";
        }
    }
    @RequestMapping("/delCollect")
    public String select(Integer id){
        collectService.delCollect(id);
        return "redirect:/selectAllByClientId";
    }



}
